package com.Arsanesia.TempleRush.Views.Screens;

import com.Arsanesia.TempleRush.TempleRush;
import com.Arsanesia.TempleRush.Controllers.Listeners.StoryClickListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;

public class StoryScreen extends AbstractScreen{

	private Array<Sprite> storySprites;
	private float stateTime;
	private TextureRegion currentFrame;
	private Animation storyAnimation;
	private String continueMessage;
	private Image currentImage;
	private TextBounds bound;
	
	public StoryScreen(TempleRush game){
		super(game);
		
	}
	
	public void show(){
		super.show();
		continueMessage = "Tap anywhere to continue...";
		atlas = new TextureAtlas(Gdx.files.internal("Images/Stories/stories.atlas"));
		storySprites = atlas.createSprites("story");
		storyAnimation = new Animation(2.25f,storySprites);
		stateTime = 0f;
		currentImage = new Image(storyAnimation.getKeyFrame(stateTime));
//		currentImage.setScaling(Scaling.stretch);
		this.addActor(currentImage);
		this.addListener(new StoryClickListener(game));
		bound = font.getBounds(continueMessage);
	}
	@Override
	public void resize(int width, int height){
		super.resize(width, height);
		//currentImage.setSize(width, height);
		
	}
	
	public void drawMessage(){
		
		this.getSpriteBatch().begin();
		font.draw(this.getSpriteBatch(), continueMessage, VIEWPORT_DEFAULTWIDTH-bound.width,VIEWPORT_DEFAULTHEIGHT-bound.height);
		this.getSpriteBatch().end();
	}
	
	
	@Override
	public void render(float delta){
		super.render(delta);
		drawMessage();
		//get the animation time
		stateTime+= Gdx.graphics.getDeltaTime();
		//get current TextureRegion at animation sequence
		currentFrame = storyAnimation.getKeyFrame(stateTime, false);
		
		//actor making
		//Image currentImage = new Image(currentFrame);
		currentImage.setDrawable(new TextureRegionDrawable(currentFrame));
//		currentImage.setWidth(Gdx.graphics.getWidth());
//		currentImage.setHeight(Gdx.graphics.getHeight());
//		stage.addActor(currentImage);
		//if finish jump to menuscreen
		if(storyAnimation.isAnimationFinished(stateTime))
		{
			game.setScreen(game.getMenuScreen());
		}
		
	}
	
}
